package com.googlecode.totallylazy.reducers;

import com.googlecode.totallylazy.Callable1;
import com.googlecode.totallylazy.Function1;
import com.googlecode.totallylazy.Functions;

import java.util.Collection;
import java.util.concurrent.Callable;

public class CollectionMap<T, R, C extends Collection<R>> extends CollectionReducer<T, R, C> {
    private final Function1<T, R> mapper;

    public CollectionMap(Callable1<? super T, ? extends R> mapper, Callable<? extends C> identity) {
        super(identity);
        this.mapper = Functions.function(mapper);
    }

    @Override
    public C call(C rs, T t) throws Exception {
        rs.add(mapper.call(t));
        return rs;
    }
}
